package com.example.myapplication0629;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class AddItemActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText editText3;
    private EditText editText4;
    private EditText editText5;
    private EditText editText6;
    private EditText editText7;
    private EditText editText8;
    private Button button3;
    private ConstraintLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_item);
        initView();

    }


    private void initView() {
        editText3 = (EditText) findViewById(R.id.editText3);
        editText4 = (EditText) findViewById(R.id.editText4);
        editText5 = (EditText) findViewById(R.id.editText5);
        editText6 = (EditText) findViewById(R.id.editText6);
        editText7 = (EditText) findViewById(R.id.editText7);
        editText8 = (EditText) findViewById(R.id.editText8);
        button3 = (Button) findViewById(R.id.button3);
        linearLayout = (ConstraintLayout) findViewById(R.id.linearLayout);

        button3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.button3:
                submit();
                break;
        }
    }

    private void submit() {
        // validate
        String editText3String = editText3.getText().toString().trim();
        if (TextUtils.isEmpty(editText3String)) {
            Toast.makeText(this, "editText3String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String editText4String = editText4.getText().toString().trim();
        if (TextUtils.isEmpty(editText4String)) {
            Toast.makeText(this, "editText4String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String editText5String = editText5.getText().toString().trim();
        if (TextUtils.isEmpty(editText5String)) {
            Toast.makeText(this, "editText5String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String editText6String = editText6.getText().toString().trim();
        if (TextUtils.isEmpty(editText6String)) {
            Toast.makeText(this, "editText6String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String editText7String = editText7.getText().toString().trim();
        if (TextUtils.isEmpty(editText7String)) {
            Toast.makeText(this, "editText7String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String editText8String = editText8.getText().toString().trim();
        if (TextUtils.isEmpty(editText8String)) {
            Toast.makeText(this, "editText8String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something
        String name= editText3.getText().toString();
        String itemDesc= editText4.getText().toString();
        String itemRemark= editText5.getText().toString();
        String price= editText6.getText().toString();
        String id= editText7.getText().toString();
        String time= editText8.getText().toString();



        Map<String,String> map = new HashMap<>();
        map.put("itemName",name);
        map.put("itemDesc",itemDesc);
        map.put("itemRemark",itemRemark);
        map.put("initPrice",price);
        map.put("kindId",id);
        map.put("avail",time);
        try {
            String result = OkHttpUtil.postRequest("http://172.18.85.254:8080/auction/api/items",map);
            System.out.println(result);
        }catch (ExecutionException e){
            e.printStackTrace();
        }catch (InterruptedException e){
            e.printStackTrace();

        }

    }
}


